package com.donger.upms.biz.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.log.annotation.Log;
import com.donger.upms.api.dto.DropDown;
import com.donger.upms.api.entity.SysRole;
import com.donger.upms.api.service.ISysRoleMenuService;
import com.donger.upms.api.service.ISysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author xyx
 * @since 2019-01-12
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController {

    private static final String MODULE_NAME = "系统角色管理";

    private final ISysRoleService sysRoleService;

    private final ISysRoleMenuService sysRoleMenuService;

    /**
     * 角色列表
     */
    @GetMapping("/page")
    public Result<IPage<SysRole>> page(Page<SysRole> page, SysRole sysRole) {
        return Res.ok(sysRoleService.page(page, new QueryWrapper<SysRole>().lambda()
                .like(StrUtil.isNotEmpty(sysRole.getRoleName()), SysRole::getRoleName, sysRole.getRoleName())
                .like(StrUtil.isNotBlank(sysRole.getRoleCode()), SysRole::getRoleCode, sysRole.getRoleCode())
        ));
    }

    @GetMapping("/list")
    public Result list(SysRole sysRole) {
        return Res.ok(sysRoleService.list(new QueryWrapper<SysRole>().lambda()
                .like(StrUtil.isNotEmpty(sysRole.getRoleName()), SysRole::getRoleName, sysRole.getRoleName())
                .like(StrUtil.isNotBlank(sysRole.getRoleCode()), SysRole::getRoleCode, sysRole.getRoleCode())
        ));
    }


    /**
     * 角色下拉框
     */
    @GetMapping("/dropdown")
    public Result<List<DropDown>> select() {
        List<DropDown> list = sysRoleService.list().stream().map(item -> {
            return new DropDown().setLabel(item.getRoleName()).setValue(item.getRoleId());
        }).collect(Collectors.toList());
        return Res.ok(list);
    }

    /**
     * 角色信息
     */
    @Log(value = "查看角色信息", moduleName = MODULE_NAME)
    @GetMapping("/info/{roleId}")
    public Result<SysRole> info(@PathVariable("roleId") Long roleId) {
        SysRole role = sysRoleService.getById(roleId);
        if (StrUtil.isNotBlank(role.getDsScope())) {
            role.setDsScopeList(Arrays.asList(role.getDsScope().split(",")));

        }
        //查询角色对应的菜单
        role.setMenuIdList(sysRoleMenuService.queryMenuIdList(roleId));
        return Res.ok(role);
    }


    /**
     * 查询角色所对应的权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/info/menu/{roleId}")
    public Result<List<Long>> menuList(@PathVariable("roleId") Long roleId) {
        return Res.ok(sysRoleMenuService.queryMenuIdList(roleId));
    }


    /**
     * 删除原来菜单  增加新菜单
     *
     * @param menuList
     * @return
     */
    @PostMapping("/info/menu/{roleId}")
    public Result saveMenu(@RequestBody List<Long> menuList, @PathVariable Long roleId) {
        sysRoleService.saveRoleMenu(menuList, roleId);
        return Res.ok("保存成功");
    }

    /**
     * 保存角色
     */
    @Log(value = "保存角色信息", moduleName = MODULE_NAME)
    @PostMapping("/save")
    public Result<Boolean> save(@Valid @RequestBody SysRole role) {
        if (role.getDsScopeList() != null) {
            role.setDsScope(String.join(",", role.getDsScopeList()));
        }
        return Res.ok(sysRoleService.save(role));
    }

    /**
     * 修改角色
     */
    @Log(value = "修改角色信息", moduleName = MODULE_NAME)
    @PostMapping("/update")
    public Result<Boolean> update(@Valid @RequestBody SysRole role) {
        if (role.getDsScopeList() != null) {
            role.setDsScope(String.join(",", role.getDsScopeList()));
        }
        return Res.ok(sysRoleService.updateById(role));
    }

    /**
     * 删除角色
     */
    @Log(value = "删除角色信息", moduleName = MODULE_NAME)
    @DeleteMapping("/delete/{roleId}")
    public Result<Boolean> delete(@PathVariable("roleId") Long roleId) {
        return Res.ok(sysRoleService.removeById(roleId));
    }

}

